package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.common.been.UserDetailBeen;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.service.CrmMemberService;
import com.kaizzi.data.jdl.service.MotionClocksService;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/16
 */
@Component
public class PageData41 {

    @Resource
    private MotionClocksService motionClocksService;
    @Resource
    private CrmMemberService crmMemberService;
    @Resource
    private PageData42 pageData42;

    public String getData(ExcelModel model){

        Page41 page41 = new Page41();

        String ytdDate = DateUtil.getYtdDate();
        List<String> transTime = DateUtil.getTransTime(ytdDate);

        List<TempBeen> yfFission = motionClocksService.getYfFission(transTime.get(0), transTime.get(1));
        List<TempBeen> aPublic = motionClocksService.getPublic(transTime.get(0), transTime.get(1));
        List<TempBeen> yfKoc = motionClocksService.getYfKoc(transTime.get(0), transTime.get(1));
        List<TempBeen> marathon = motionClocksService.getMarathon(transTime.get(0), transTime.get(1));
        List<TempBeen> wes = motionClocksService.getWes(transTime.get(0), transTime.get(1));

        if (yfFission== null ||yfFission.size() == 0){
            page41.setYfFission(getEmptyBeen(1));
        } else {
            page41.setYfFission(getBeen(yfFission,1));
        }

        if (aPublic == null || aPublic.size() == 0){
            page41.setAPublic(getEmptyBeen(2));
        }else {
            page41.setAPublic(getBeen(aPublic,2));
        }

        if (yfKoc == null || yfKoc.size() == 0){
            page41.setYfKoc(getEmptyBeen(3));
        }else {
            page41.setYfKoc(getBeen(yfKoc,3));
        }

        if (marathon == null || marathon.size() == 0){
            page41.setMarathon(getEmptyBeen(4));
        }else {
            page41.setMarathon(getBeen(marathon,4));
        }

        if (wes == null || wes.size() == 0){
            page41.setWes(getEmptyBeen(5));
        }else {
            page41.setWes(getBeen(wes,5));
        }

        page41.setTitle(LogicUtil.getTitle(model,41));
        page41.setDataPeriod(DateUtil.getFirstDay2LastSunday());

        return JsonUtil.serialize(page41);
    }

    private TempPage41 getBeen(List<TempBeen> list,int no){
        TempPage41 tempPage41 = new TempPage41();
        tempPage41.setChildTitle(getChildTitle(pageData42.getSingleName(no)));
        double sum = list.stream().mapToDouble(TempBeen::getTempCount).sum();
        tempPage41.setClockInCount(StringUtil.pointFormat(sum));
        tempPage41.setClockInUsers(StringUtil.pointFormat(list.size()));

        List<String> list1 = new ArrayList<>();
        List<UserDetailBeen> userDetail = crmMemberService.getCrmUserDetail(list.get(0).getTempName());
        String content1 = "打卡最多的用户打卡了"+list.get(0).getTempCount()+"次";
        String content2 = "地区："+userDetail.get(0).getCity()+"市"+userDetail.get(0).getArea()+"区";
        String sportTypes = getSportTypes(userDetail.get(0).getMotionLike());
        String content3 = "喜爱的运动类型："+sportTypes;
        list1.add(content1);
        list1.add(content2);
        list1.add(content3);

        tempPage41.setTexts(list1);
        String rate = StringUtil.decimalFormat(list.size() / pageData42.getSingleCount(no) * 100);
        tempPage41.setInteractiveRate(rate+"%");
        return tempPage41;
    }

    private TempPage41 getEmptyBeen(int no){
        TempPage41 tempPage41 = new TempPage41();
        tempPage41.setClockInCount("0");
        tempPage41.setClockInUsers("0");
        tempPage41.setInteractiveRate("0%");
        tempPage41.setTexts(new ArrayList<>());
        tempPage41.setChildTitle(getChildTitle(pageData42.getSingleName(no)));
        return tempPage41;
    }

    private String getChildTitle(String childTitle){
        return childTitle.replace("（","（'").replace("）","'）");
    }

    private String getSportTypes(String types){
        if (types == null || types.isEmpty()){
            return "篮球";
        }

        String[] split = types.split(",");
        List<String> sports = new ArrayList<>();
        for (String s : split) {
            sports.add(getSportTypeName(s));
        }
        return String.join(",", sports);
    }

    /**
     * @param type 运动类型
     * @return 运动中文名称
     */
    private String getSportTypeName(String type){
        String result = "";
        switch (type){
            case "1":
                result = "羽毛球";
                break;
            case "2":
                result = "足球";
                break;
            case "3":
                result = "篮球";
                break;
            case "4":
                result = "跑步";
                break;
            case "5":
                result = "健身";
                break;
            case "6":
                result = "其他";
                break;
            default:
                break;
        }
        return result;
    }

    @Data
    private static class TempPage41{
        private String childTitle;
        private String clockInCount;
        private String clockInUsers;
        private String interactiveRate;
        private List<String> texts;
    }

    @Data
    private static class Page41{
        private String title;
        private String dataPeriod;
        private TempPage41 yfFission;
        private TempPage41 aPublic;
        private TempPage41 yfKoc;
        private TempPage41 marathon;
        private TempPage41 wes;
    }
}
